<style scoped>
* {
  padding: 0;
  margin: 0;
}

.center {
  width: 100%;
  height: 958px;
  background-image: url('../assets/login/login.jpg');
  background-size: cover;
  background-repeat: no-repeat;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

h1 {
  font-size: 30px;
  color: black;
}

.logon {
  background-color: #fff;
  border-radius: 10px;
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.25), 0 10px 10px rgba(0, 0, 0, 0.22);
  /* position: relative;
  overflow: hidden; */
  width: 768px;
  max-width: 100%;
  min-height: 480px;
  margin-top: 20px;
  display: flex;
  background: -webkit-linear-gradient(right, #4284db, #29eac4);
}

.overlaylong {
  border-radius: 10px 0 0 10px;
  width: 50%;
  height: 100%;
  background-color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
}


.overlaytitle {
  border-radius: 0px 10px 10px 0px;
  width: 50%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0);
  display: flex;
  align-items: center;
  justify-content: center;
}


.overlaytitleH2 {
  font-size: 30px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitleP {
  font-size: 15px;
  color: #fff;
  margin-top: 20px;
}

.overlaytitle-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

.buttongohs {
  width: 180px;
  height: 40px;
  border-radius: 50px;
  border: 1px solid #fff;
  color: #fff;
  font-size: 15px;
  text-align: center;
  line-height: 40px;
  margin-top: 40px;
}

.overlaylongH2 {
  font-size: 25px;
  color: black;
  /* width: 250px; */
}

.overlaylong-Signin {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
}

input {
  background-color: #eee;
  border: none;
  padding: 12px 15px;
  margin: 10px 0;
  width: 240px;
}

h3 {
  font-size: 14px;
  margin-top: 10px;
  cursor: pointer;
}

.inupbutton {
  background-color: #29eac4;
  border: none;
  width: 180px;
  height: 40px;
  border-radius: 50px;
  font-size: 15px;
  color: #fff;
  text-align: center;
  line-height: 40px;
  margin-top: 30px;
}
</style>

<template>
  <div>
    <div class="center">
      <h1>登录/
        <router-link style="text-decoration: none;color:#000;" to="/">回首页</router-link>
      </h1>
      <div class="logon">
        <div class="overlaylong">
          <div class="overlaylong-Signin">
            <h2 class="overlaylongH2">登录</h2>
            <el-input prefix-icon="el-icon-user-solid" type="text" placeholder="账号" v-model:value="state.user"/>
            <div style="height: 10px"></div>
            <el-input prefix-icon="iconfont icon-mima" type="password" placeholder="密码"
                      v-model:value="state.password"/>
            <div style="margin-top: 10px;width: 200px;display: flex;justify-content: space-between">
              <el-input v-model:value="state.code" style="width: 130px;" placeholder="验证码"/>
              <el-image :src="img" @click="refresh"/>
            </div>
            <div style="margin-top: 10px;width: 200px;display: flex;justify-content: space-evenly">
              <el-radio v-model:value="state.role" label="user">用户</el-radio>
              <el-radio v-model:value="state.role" label="admin">管理员</el-radio>
            </div>

            <h3 style="cursor: pointer" @click="toRestPassword">忘记密码？</h3>
            <button class="inupbutton" @click="login">登录</button>
          </div>

        </div>

        <div class="overlaytitle">
          <div class="overlaytitle-Signin">
            <h2 class="overlaytitleH2">你好！</h2>
            <p class="overlaytitleP">
              输入您的个人信息，与我们一起开始吧！
            </p>
            <div class="buttongohs" @click="register">注册</div>
          </div>
        </div>
      </div>

    </div>
  </div>

</template>

<script>
import axios from "axios";
import {saveToken} from "@/store/token";

export default {
  data() {
    return {
      state: {
        user: "",
        password: "",
        role: 'user',
        codeKey: new Date().getTime() + Math.round(20)
      },
      img: "",

    }
  },
  methods: {
    login() {
      //发送请求给后端，后端拿到数据给返回结果
      this.$api.user.login(this.state).then((res) => {
        var data = res.data;
        if (data.success) {
          this.$message({
            message: '登录成功' + this.state.role,
            type: 'success'
          });
          saveToken('Authorization', res.data.content.token);
          saveToken('user', JSON.stringify(res.data.content.user));
          saveToken('role', this.state.role);
          saveToken("tokenStartTime", new Date().getTime())
          if (this.state.role === "admin") {
            this.$router.push('/admin');
          } else {
            this.$router.push({path: '/'});
          }
        } else {
          this.$message.error(res.data.message?res.data.message:'账号或者密码错误');
        }
      })
    },
    register() {
      this.$router.push({path: '/register'});
    },
    refresh () {
      this.img = 'http://localhost:8088/user/createCode?time=' + Date.now() + '&codeKey=' + this.state.codeKey
    },
    toRestPassword(){
      this.$router.push("/resetPassword")
    }
  },
  mounted() {
    this.refresh()
  }
}
</script>